<?php
namespace app\index\controller;

use app\FrameBaseController;
use think\facade\View;
use think\facade\Config;
use app\validate\ValidateQQ;
use think\exception\ValidateException;
//use think\facade\Session;
//use think\facade\Request;

class QQ extends FrameBaseController 
{
    /**
     * QQ服务
     */
    public function index()
    {
        $result = get_curr_user();
        if ($result['status'] == -1) {
            return $this->isNotLogin('QQ');
        }
        if ($result['id'] != 1) {
            return $this->isNotLogin('QQ', '超级管理员');
        }
        $serviceList = Config::get('qq.qqlist');
        foreach ($serviceList as $key => $value) {
            unset($serviceList[$key]['name']);
            $serviceList[$key] = array_keys($serviceList[$key]);
        }
        View::assign('QQConfig', $serviceList);
        return $this->frameTpl('QQ');
    }

    /**
     * QQ服务api
     */
    public function api($apiname)
    {
        $result = get_curr_user();
        if ($result['status'] == -1) {
            return json_encode([
                'errno' => -1,
                'msg'   => '请登录!',
                'id'    => -1,
            ]);
        }
        if ($result['id'] != 1) {
            return json_encode([
                'errno' => -1,
                'msg'   => '请登录超级管理员账号!',
                'id'    => -1,
            ]);
        }
        $serviceList = Config::get('qq.qqlist');
        $varList     = Config::get('qq.varlist');
        $serverIp    = Config::get('qq.server_ip');
        $serverPort  = Config::get('qq.server_port');
        if (!isset($serviceList[$apiname])) {
            return json_encode([
                'errno' => -1,
                'msg'   => '请输入完整参数!',
                'id'    => -1,
            ]);
        }
        $data = [];
        foreach (input('post.') as $key => $value) {
            if ('' != $value) {
                $data = array_merge($data, [$key => $value]);
            }
        }
        foreach ($serviceList[$apiname] as $key => $value) {
            if ('name' == $key) {
                continue;
            }
            if (!isset($data[$key])) {
                return json_encode([
                    'errno' => -1,
                    'msg'   => '请输入完整参数!',
                    'id'    => -1,
                ]);
            }
            try {
                validate(ValidateQQ::class)
                    ->check([
                        $value => $data[$key]
                    ]);
            } catch (\Exception $e) {
                if (isset($varList[$key]['default'])) {
                    $data[$key] = $varList[$key]['default'];
                } else {
                    return json_encode([
                        'errno' => 32,
                        'msg'   => '格式错误!',
                        'id'    => -1,
                    ]);
                }
            }
        }
        $requestUrl = $serverIp . ':' . $serverPort . '/' . $apiname . '?' . http_build_query($data);
        $result = json_decode(file_get_contents($requestUrl), true);
        $returnList = Config::get('qq.returnlist');
        $res = [
            'errno' => $result['retcode'],
            'msg'   => $result['status'],
        ];
        foreach ($returnList[$apiname] as $key => $value) {
            $res[$key] = get_array_value($result, $value);
        }
        return json_encode($res);
    }
}
